package com.snow4cloud.common.redis.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 *
 * @author fanxl
 */
@Component
public class RedisUtils {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ValueOperations<String, String> valueOperations;
    @Autowired
    private HashOperations<String, String, Object> hashOperations;
    @Autowired
    private ListOperations<String, Object> listOperations;
    @Autowired
    private SetOperations<String, Object> setOperations;

    /**
     * 默认过期时长，单位：秒
     */
    public static final long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**
     * 不设置过期时长
     */
    public static final long NOT_EXPIRE = -1;

    public void set(String key, Object value, long expire) {
        valueOperations.set(key, toJson(value));
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.MINUTES);
        }
    }

    public void setAdd(String key, Object... value) {
        setOperations.add(key, value);
    }

    public Set<Object> setMembers(String key) {
        return setOperations.members(key);
    }

    public void setRemove(String key, Object... value) {
        setOperations.remove(key, value);
    }

    public long setSize(String key) {
        return setOperations.size(key);
    }

    public void listLeftPush(String key, Object value) {
        listOperations.leftPush(key, value);
    }

    /**
     * 移除list中所有与 value 相等的值。
     */
    public void listRemove(String key, Object value) {
        listOperations.remove(key, 0, value);
    }

    public List<Object> listRange(String key) {
        return listOperations.range(key, 0, -1);
    }

    public void hashPutAll(String key, Map<String, String> map) {
        hashOperations.putAll(key, map);
    }

    public void hashDelete(String key, Object... objects) {
        hashOperations.delete(key, objects);
    }

    public Boolean hashHasKey(String key, Object objects) {
        return hashOperations.hasKey(key, objects);
    }

    public Map<String, Object> hashEntries(String key) {
        return hashOperations.entries(key);
    }

    public void set(String key, Object value) {
        set(key, value, DEFAULT_EXPIRE);
    }

    public <T> T get(String key, Class<T> clazz, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.MINUTES);
        }
        return value == null ? null : fromJson(value, clazz);
    }

    public <T> T get(String key, Class<T> clazz) {
        return get(key, clazz, NOT_EXPIRE);
    }

    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.MINUTES);
        }
        return value;
    }

    public String get(String key) {
        return get(key, NOT_EXPIRE);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    /**
     * Object转成JSON数据
     */
    private String toJson(Object object) {
        if (object instanceof Integer
                || object instanceof Long
                || object instanceof Float
                || object instanceof Double
                || object instanceof Boolean
                || object instanceof String) {
            return String.valueOf(object);
        }
        return JSON.toJSONString(object);
    }

    /**
     * JSON数据，转成Object
     */
    private <T> T fromJson(String json, Class<T> clazz) {
        return JSON.parseObject(json, clazz);
    }
}
